package com.learning.spring.cloud.alibaba.registraor.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

/**
 * ClassName: SecuritySecureConfig
 * Description: 安全配置
 * Date: 2019/5/31 10:42 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Configuration
@EnableWebSecurity
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().anyRequest().permitAll()
                .and()
                .httpBasic()
                .and()
                // 使用cookie启用CSRF-Protection
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringRequestMatchers(// 为服务注册等禁用 CSRF-Protection。
                        // RestTemplateEurekaHttpClient 客户端工具类可以查看服务路径
                        // 如注册 http://admin:admin123@localhost:8761/eureka/apps/03-MS-REGISTRAOR-EUREKA-CLIENT
                        new AntPathRequestMatcher("/eureka/apps/**")
                ).and()
                .rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600);
    }

}